# Converting rules.

include REXML

module ConverterRuleset

	class Ignore
	  def execute(post, e)
		return e
	  end
	end

	class Rename
	  def initialize(name, &callback)
		@name = name
		@callback = callback
	  end
	  
	  def execute(post, e)
		e.name = @name
		e.text = @callback.call(e.text) unless @callback.nil?
		
		return e
	  end
	end

	class Multify
	  def initialize(list, &callback)
		@list = list
		@callback = callback
	  end
	  
	  def execute(post, e)
		post.delete_element e 
		@list.each do |name|
		  element = Element.new(name, post)
		  element.text = @callback.call(e.text) unless @callback.nil?
		end
		
		return e
	  end
	end

	class Sequence < Rename
	  @@initial_value = 1
	  
	  def execute(post, e)
		@@initial_value += 1
		element = Element.new(@name, post)
		element.text = @@initial_value
		
		element
	  end
	end

	class NewElement
	  def initialize(parent_name, name)
		@parent_name = parent_name
		@name = name
	  end
	  
	  def execute(post, e)
		parent = Element.new(@parent_name, post)
		Element.new(@name, parent).text = e.text
		
		return parent
	  end
	end

	class Strip < Rename
	  def execute(post, e)
		e.name = @name
		first_child = e.elements[1]
		if first_child
		  e.text = first_child.text
		  e.delete_element(first_child)
		end

		return e
	  end
	end
end
